package com.example.a2212park

import android.content.Intent
import android.os.CountDownTimer
import android.util.Log
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationListener
import com.amap.api.location.IReGeoLocationCallback
import com.amap.api.maps.AMap
import com.amap.api.maps.MapView
import com.amap.api.maps.model.MyLocationStyle
import com.amap.api.services.core.LatLonPoint
import com.amap.api.services.geocoder.GeocodeResult
import com.amap.api.services.geocoder.GeocodeSearch
import com.amap.api.services.geocoder.RegeocodeQuery
import com.amap.api.services.geocoder.RegeocodeResult
import com.blankj.utilcode.util.SPUtils
import com.example.a2212park.databinding.ActivitySplashBinding
import com.example.common.view.adapter.ImageBannerAdapter
import com.example.common.view.view.BaseMvvmActivity
import com.example.db.entity.BannerEntity
import com.example.image.glide.GlideImgLoader
import com.example.mine.MineActivity
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
//首页倒计时页面
class SplashActivity : BaseMvvmActivity<ActivitySplashBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_splash
    private var bannerData= mutableListOf<BannerEntity>()
    private lateinit var bannerAdater:ImageBannerAdapter
    private lateinit var mCountTimer:CountDownTimer

    lateinit var map: AMap
    lateinit var mapView: MapView
    lateinit var myLocationStyle: MyLocationStyle
    lateinit var geocoderSearch: GeocodeSearch
    lateinit var query: RegeocodeQuery

    override fun initView() {
        requestPermissions(
            arrayOf(
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.ACCESS_FINE_LOCATION
            ),101)
        mapView=MapView(this)
        map=mapView.map
        mapView.onCreate(null)
        myLocationStyle=MyLocationStyle()
        myLocationStyle.interval(2000)
        map.myLocationStyle=myLocationStyle
        map.isMyLocationEnabled=true
        map.addOnMyLocationChangeListener {
            geocoderSearch=GeocodeSearch(this)
            query = RegeocodeQuery(LatLonPoint(it.latitude,it.longitude), 200f, GeocodeSearch.AMAP)
            geocoderSearch.getFromLocationAsyn(query)
            geocoderSearch.setOnGeocodeSearchListener(object : GeocodeSearch.OnGeocodeSearchListener {
                override fun onRegeocodeSearched(p0: RegeocodeResult?, p1: Int) {
                    Log.d("123123123","定位:"+p0?.regeocodeAddress?.formatAddress+"坐标:"+p1)
                    SPUtils.getInstance().put("gaode",p0?.regeocodeAddress?.formatAddress)
                }

                override fun onGeocodeSearched(p0: GeocodeResult?, p1: Int) {

                }

            })
        }
        mapView


        var isFisert=MMKV.defaultMMKV().decodeBool("isFisert",true)
        if (isFisert){
            MMKV.defaultMMKV().encode("isFisert",false)
            showAppBanner()
        }else{
            showAppAdv()
        }
    }
    //展示主图
    private fun showAppBanner() {
        //View.VISIBLE表示显示UI
        //View.GONE表示既不显示UI也不占据UI大小和位置
        //View.INVISIBLE表示不显示UI但可以占据UI大小和位置
        mDataBinding.clAppBanner.visibility=View.VISIBLE
        mDataBinding.clAppAdv.visibility=View.GONE
        bannerData.run{
            add(BannerEntity(0,R.drawable.ic_guang4,""))
            add(BannerEntity(1,R.drawable.ic_guang2,""))
            add(BannerEntity(2,R.drawable.ic_guang3,""))
        }
        bannerAdater=ImageBannerAdapter(bannerData)
        mDataBinding.appBanner.run {
            setAdapter(bannerAdater)
            //关联生命周期
            addBannerLifecycleObserver(this@SplashActivity)
            //设置指示器
            indicator=CircleIndicator(this@SplashActivity)
            //设置页面切换效果
            addPageTransformer(AlphaPageTransformer())
            //设置页面监听器
            addOnPageChangeListener(object :OnPageChangeListener{
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {

                }

                override fun onPageSelected(position: Int) {
                    //判断Banner是否到最后一页
                    if(position ==bannerData.size-1){
                        mDataBinding.tvStart.visibility=View.VISIBLE
                    }else{
                        mDataBinding.tvStart.visibility=View.GONE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) {

                }
            })
        }
        //点击进入
        mDataBinding.tvStart.setOnClickListener {
            jumMainPage()
        }
    }
    //展示广告
    private fun showAppAdv() {
        mDataBinding.clAppBanner.visibility=View.GONE
        mDataBinding.clAppAdv.visibility=View.VISIBLE
        //显示广告图片
        mDataBinding.ivAdv.run {
            GlideImgLoader.loadResImage(this, R.drawable.ic_guang1,this)
        }
        //5秒倒计时
        mCountTimer=object :CountDownTimer(5000,1000){
            override fun onTick(millisUntilFinished: Long) {
                mDataBinding.tvAdvSkip.text="${millisUntilFinished/1000}S跳过"
            }

            override fun onFinish() {
                this@SplashActivity.jumMainPage()
            }

        }
        //启动倒计时
        mCountTimer.start()
    }


    override fun onDestroy() {
        super.onDestroy()
        mCountTimer?.cancel()
    }

    private fun jumMainPage() {
        ARouter.getInstance().build("/main/MainActivity").navigation()
        finish()
    }

}